//
//  P2036.cpp
//  Algorithm
//
//  Created by 刘锦程 on 2023/9/11.
//

#include <stdio.h>
#include<iostream>
using namespace std;
int n;
struct node{
    int s;
    int b;
};
node t[12];
int book[12];
int suan=1,ku;
int ans=2147483647;
void dfs(int step,int div){
    if(div<ans&&step>0) ans=div;
    if(step==n){
        return;
    }else{
        for(int i=1;i<=n;i++){
            if(!book[i]){
                book[i]=1;
                suan*=t[i].s;
                ku+=t[i].b;
                dfs(step+1,abs(suan-ku));
                ku-=t[i].b;
                suan/=t[i].s;
                book[i]=0;
                
            }
        }
        
    }
    
    
}
int P2036ans(){
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d %d",&t[i].s,&t[i].b);
    }
    dfs(0,0);
    cout<<ans<<endl;
    return 0;
}
